<?php
// +----------------------------------------------------------------------
// | layerIM + Workerman + ThinkPHP5 即时通讯
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Config;
use app\common\controller\HomeBase;
// 聊天记录表格
use app\common\model\Aimsgs as AimsgsModel;
// 用户画像数据表
use app\common\model\Aiusersinfo as AiuserInfoModel;
// 授权的微信公众号
use app\common\model\AuthorWxUser as AuthourWxUserModel;
//  话术表
use app\common\model\Aimsgrules as AimsgrulesModel;
// 新增后台试用账号
use app\common\model\AdminUser as AdminUserModel;
use app\common\model\AuthGroupAccess as AuthGroupAccessModel;
use think\Session;

class Index extends HomeBase
{
    private $aimsgs_model;
    private $aiuserinfo_model;
    private $author_wx_user_model;
    private $aimsg_rule_model;
    protected $admin_user_model;
    protected $auth_group_access_model;
    protected function _initialize(){
        $this->aimsgs_model = new AimsgsModel();
        $this->aiuserinfo_model = new AiuserInfoModel();
        $this->author_wx_user_model = new AuthourWxUserModel();
        $this->aimsg_rule_model = new AimsgrulesModel();
        $this->admin_user_model   = new AdminUserModel();
        $this->auth_group_access_model = new AuthGroupAccessModel();
    }
    // 通过appid查找对应的wxtype, 然后通过wxtype去搜索话术表格里面的话术
    public function wxtype_words($appid=0,$value='',$msgtype=0){
        $wxtype = $this->author_wx_user_model->where('appid',$appid)->value('wxtype');
//        var_dump($wxtype);
        $map = [
            'typeid'=>$wxtype,
            'msgtype' => $msgtype
        ];
        $words = $this->aimsg_rule_model->where($map)->value($value);
//        var_dump($words);
        return $words;
    }

    // 传入参数返回对应的话术
    public function wxtype_words2($appid=0,$value='',$msgtype=0){
        $wxtype = $this->author_wx_user_model->where('appid',$appid)->value('wxtype');
        var_dump($wxtype);
        $map = [
            'typeid'=>$wxtype,
            'msgtype' => $msgtype
        ];
        $words = $this->aimsg_rule_model->where($map)->value($value);
        var_dump($words);
    }

    public function index()
    {
        return $this->fetch();
    }

    public function save_user(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $where['password'] = md5($data['password'] . Config::get('salt'));
            $where['create_time'] = date('Y-m-d H:i:s');
            $admin_add = $this->admin_user_model->allowField(true)->save($data);
            if ($admin_add) {
                $auth_group_access['uid']      = $this->admin_user_model->id;
                $auth_group_access['group_id'] = 3;
                $this->auth_group_access_model->save($auth_group_access);
//                Session::set('admin_id', $admin_add);
//                Session::set('admin_name', $where['username']);
                $this->success('注册成功','/admin2/');
//                return json_encode(['status' => 1, 'msg' => '注册成功', 'send_time' => time()], JSON_UNESCAPED_UNICODE);
            }else{
                $this->error('注册失败');
//                return json_encode(['status' => 404, 'msg' => '注册失败', 'send_time' => time()], JSON_UNESCAPED_UNICODE);
            }
        }
    }

    
}
